746D - Green and Black Tea - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

import sys 
sys.setrecursionlimit(1000000000)
import math
import heapq
import time 
from collections import deque
from collections import defaultdict
mod = 10**9+7
def solve(n,k,a,b):
    if k*(min(a,b)+1)<max(a,b):
        return "NO"
    res = ""
    turn = 0
    while max(a,b)-min(a,b)>=k:
        if turn%2==0:
            if max(a,b)==a:
                res += "G"*k 
                a-=k
            else:
                res += "B"*k 
                b-=k
        else:
            if min(a,b)==a:
                res += "G"
                a -=1
            else:
                res += "B"
                b-=1
        turn +=1
   
    if len(res)==0:
        if max(a,b) == a:
            res += "G"
        else:
            res += "B"
    while min(a,b)>0:
        if res[-1]=="G":
            res+= "B"
            b-=1
        else:
            res += "G"
            a-=1
    while len(res)<n:
        if max(a,b)==a:
            res += "G"
        else:
            res += "B"
    return res


    




n,k,a,b = map(int,input().split())
res2 = solve(n,k,a,b)
print(res2)




C++ Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <utility>
#include <complex>
#define ff first
#define ss second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF=1e9+250;
LL mod=1e9+7;
const int N=100005;
char ans[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	int n,m,a,b;
	cin>>n>>m>>a>>b;
	int pos=0;
	int ta=0,tb=0;
	while(a>0||b>0) {
		if(a<b) {
			if(tb<m) {
				ans[pos]='B';
				tb++;
				ta=0;
				b--;
			} else {
				if(a>0&&ta<m) {
					ta++;
					tb=0;
					ans[pos]='G';
					a--;
				} else {
					puts("NO");
					return 0;
				}
			}
 
		} else if(a>b) {
			if(ta<m) {
				ta++;
				tb=0;
				ans[pos]='G';
				a--;
			} else {
				if(b>0&&tb<m) {
					tb++;
					ta=0;
					ans[pos]='B';
					b--;
				} else {
					puts("NO");
					return 0;
				}
			}
		} else {
			if(ta<m) {
				ta++;
				tb=0;
				ans[pos]='G';
				a--;
			} else if(tb<m) {
				tb++;
				ta=0;
				ans[pos]='B';
				b--;
			} else {
				puts("NO");
				return 0;
			}
		}
		pos++;
	}
	puts(ans);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam